#include <cstdio>
#include <vector>
#include <functional>
#include <algorithm>
#include "lex.h"
#include "symbolTable.h"
#include <iostream>

using namespace cp;

FILE *finput, *foutput;
char nextChar(){
	++cp::col;
	char ret = fgetc(finput);
	if (ret == '\n'){
		++row;
		col = 0;
	}
	return ret;
}
char (*cp::nextChar)() = ::nextChar;
int cp::row = 1, cp::col = 0;


int main(int argc, char** argv){
	if (argc < 2) {
		printf("参数不足\n");
		return -1;
	}

	finput = fopen(argv[1], "r");
	if (!finput){
		std::cout << "无法打开指定的输入文件" << std::endl;
		return -1;
	}

	initSymbolAndProds();
	initPDA();
	if (!beginParse()){
		std::cout << "翻译失败" << std::endl;
		return -1;
	}

	if (argc < 3){
		std::cout << "未指定输出文件，改向控制台输出" << std::endl;
		foutput = stdout;
	}
	else {
		foutput = fopen(argv[2], "w");
		if (!foutput){
			std::cout << "无法打开指定的输出文件，改向控制台输出" << std::endl;
			foutput = stdout;
		}
	}
	printQuads(foutput);

	fclose(finput);
	if (foutput != stdout) fclose(foutput);
	return 0;
}











